# -*- encoding:utf-8 -*-

from service.sharerank.ShareRankCrawler import ShareRankCrawler
import time
__author__ = 'shudong.msd'

# 写Excel
def writeToExcel(filepath):
    print u'开始 sharerank 排行榜信息 抓取 写入excel'

    crawler = ShareRankCrawler()

    # 输出关注排行榜 新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=follow7d&current=ALL&pct=ALL&follow7d=ALL')
    crawler.writeToExcel(filepath,0,0)
    time.sleep(0.5)

    # # 输出关注排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=follow&current=ALL&pct=ALL&follow=ALL')
    crawler.writeToExcel(filepath,1,1)
    time.sleep(0.5)

    #
    # # 输出讨论排行榜 新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=tweet7d&current=ALL&pct=ALL&tweet7d=ALL')
    crawler.writeToExcel(filepath,2,2)
    time.sleep(0.5)

    # # 输出讨论排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=tweet&current=ALL&pct=ALL&tweet=ALL')
    crawler.writeToExcel(filepath,3,3)
    time.sleep(0.5)

    # # 输出分享交易排行榜_新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=deal7d&current=ALL&pct=ALL&deal7d=ALL')
    crawler.writeToExcel(filepath,4,4)
    time.sleep(0.5)

    # # 输出分享交易排行榜_最热门
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=deal&current=ALL&pct=ALL&deal=ALL')
    crawler.writeToExcel(filepath,5,5)


# 写DB
def writeToDB():
    print u'开始 sharerank 排行榜信息 抓取 写入db'

    crawler = ShareRankCrawler()

    # 输出关注排行榜 新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=follow7d&current=ALL&pct=ALL&follow7d=ALL')
    crawler.writeToDB(0)
    time.sleep(0.5)

    # # 输出关注排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=follow&current=ALL&pct=ALL&follow=ALL')
    crawler.writeToDB(1)
    time.sleep(0.5)

    #
    # # 输出讨论排行榜 新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=tweet7d&current=ALL&pct=ALL&tweet7d=ALL')
    crawler.writeToDB(2)
    time.sleep(0.5)

    # # 输出讨论排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=tweet&current=ALL&pct=ALL&tweet=ALL')
    crawler.writeToDB(3)
    time.sleep(0.5)

    # # 输出分享交易排行榜_新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=deal7d&current=ALL&pct=ALL&deal7d=ALL')
    crawler.writeToDB(4)
    time.sleep(0.5)

    # # 输出分享交易排行榜_最热门
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=deal&current=ALL&pct=ALL&deal=ALL')
    crawler.writeToDB(5)


# 写DB
def writeToDB_hot_asc():
    print u'开始 sharerank 热度排行榜信息 升序抓取，补充遗漏'

    crawler = ShareRankCrawler()

    # # 输出关注排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=asc&orderby=follow&current=ALL&pct=ALL&follow=ALL')
    crawler.writeToDB(1,True)
    time.sleep(0.5)



    # # 输出讨论排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=asc&orderby=tweet&current=ALL&pct=ALL&tweet=ALL')
    crawler.writeToDB(3,True)
    time.sleep(0.5)

    # # 输出分享交易排行榜_最热门
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=asc&orderby=deal&current=ALL&pct=ALL&deal=ALL')
    crawler.writeToDB(5,True)


# 更新DB
def updateDB():
    print u'开始 sharerank 排行榜信息 更新'

    crawler = ShareRankCrawler()

    # 输出关注排行榜 新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=follow7d&current=ALL&pct=ALL&follow7d=ALL')
    crawler.updateDB(0)
    time.sleep(0.5)

    # # 输出关注排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=follow&current=ALL&pct=ALL&follow=ALL')
    crawler.updateDB(1)
    time.sleep(0.5)

    #
    # # 输出讨论排行榜 新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=tweet7d&current=ALL&pct=ALL&tweet7d=ALL')
    crawler.updateDB(2)
    time.sleep(0.5)

    # # 输出讨论排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=tweet&current=ALL&pct=ALL&tweet=ALL')
    crawler.updateDB(3)
    time.sleep(0.5)

    # # 输出分享交易排行榜_新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=deal7d&current=ALL&pct=ALL&deal7d=ALL')
    crawler.updateDB(4)
    time.sleep(0.5)

    # # 输出分享交易排行榜_最热门
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=deal&current=ALL&pct=ALL&deal=ALL')
    crawler.updateDB(5)


def updateDB_hot_asc():
    print u'开始 sharerank 热度排行榜信息 更新 '

    crawler = ShareRankCrawler()

    # # 输出关注排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=asc&orderby=follow&current=ALL&pct=ALL&follow=ALL')
    crawler.updateDB(1)
    time.sleep(0.5)



    # # 输出讨论排行榜 最热
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=asc&orderby=tweet&current=ALL&pct=ALL&tweet=ALL')
    crawler.updateDB(3)
    time.sleep(0.5)

    # # 输出分享交易排行榜_最热门
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=asc&orderby=deal&current=ALL&pct=ALL&deal=ALL')
    crawler.updateDB(5)


# 测试访问情况
def test_get_page():
    crawler = ShareRankCrawler()

    # 输出关注排行榜 新增
    crawler.set_target(r'http://xueqiu.com/stock/screener/screen.json?category=SH&order=desc&orderby=follow7d&current=ALL&pct=ALL&follow7d=ALL')
    return crawler.get_final_list()